package com.wwg.threadSort;

import java.util.concurrent.CompletableFuture;

/**
 * @author: wwg
 * @date: 2022/3/11 7:04
 */


/**
 * join控制线程顺序执行
 */
public class ThreadSortTest {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            //join();
            completableFuture();

        }


    }


    public static void completableFuture() {
        Thread t1 = new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "开始时间：" + System.nanoTime());
            System.out.println(Thread.currentThread().getName() + "执行");
        },"t1");

        Thread t2 = new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "开始时间：" + System.nanoTime());
            System.out.println(Thread.currentThread().getName() + "执行");
        },"t2");

        Thread t3 = new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "开始时间：" + System.nanoTime());
            System.out.println(Thread.currentThread().getName() + "执行");
            System.out.println("=====================");
        },"t3");

        CompletableFuture.runAsync(t1::start)
                .thenRun(t2::start)
                .thenRun(t3::start);

    }

    public static void join() {
        Thread t1 = new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "执行");
        },"t1");

        Thread t2 = new Thread(()->{
            try {
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "执行");
        },"t2");

        Thread t3 = new Thread(()->{
            try {
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "执行");
        },"t3");

        t1.start();
        t2.start();
        t3.start();
        System.out.println("=====================");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
        if(t1.isInterrupted()) {
            return;
        }
        t2.interrupt();
        if(t2.isInterrupted()) {
            return;
        }
        t3.interrupt();
        if(t3.isInterrupted()) {
            return;
        }

    }


}
